NewPaletteMapImage
ImageIndex = NewPaletteMapImage(Width, Height)
 
Parameters:

    Width = Width of image in pixels
    Height = Height of image in pixels
Returns:

    ImageIndex = The index of this image.
 

     The NewPaletteMapImage function creates a blank 16bit image in FX format. The only difference to say creating the image with say the CreateImage / NewImage commands, is that this image is always going to be 16bit regardless of what display screen mode depth is set to. Normally images would use whatever depth the screen currently is, so if the screen is 32bit screen depth, then all images use the same format.

      We need the image to be 16bit so we can use the DrawPaletteMappedStrip16 function to actually render it behind the scenes. Using a regular PlayBASIC image for our palette mapped image, means we can also use the DrawIMage / Sprites features to render other similar formatted images onto each other. The only exception is that blend modes like Alpha Blending won't work, since those render methods will treat the pixels they blend and RGB formatted pixels and not the conceptual colour images we're thinkinf of them when palette mapping




FACTS:


      * NewPaletteMapImage creates 16bit FX image image maximum number of colours in the final palette

      * NewPaletteMapImage colour index zero is assumed to be the transparent colour.

      * Palette mapping can be used to create a number of effects from shadows, raster bars, glenz vectors, water/rain animations etc etc. It's really just up to the creativity of the programmer.




Example #1: Shadows / Colour Swapping


      This example recreates how shadow effects are done with palette mapped displays. The example creates circular banded effect using colours 0 to 1024 in the palette much like the example above. The first 1024 colours are considered the standard colours in our mock up scene, but the create palette routine also fills in alternatives colours between 1024 and 2048. Since our screen is using the first 1024 colours, to swap any pixel to the second colour bank we use the OR inkmode and OR the pixels with 1024. This effectively make those pixels colour index be in the 1024 to 2048 range. so they appear completely different colours. To make shadows we would 1/2 or dim the brightness of those colours.


  
  
  // Include the palette Mapping library
  #Include "PaletteMapping"
  
  
  // Define a palette big enough to hold 2^16 colours
  Dim Palette($10000)
  
  // Give palette mapping library address of the palette
  // we're using.
  SetPalette(Palette())
  
  For lp=0 To 1024
     Angle=Mod(lp,180)
     Level=127+Sin(Angle)*127
     Level=RGB(Level,LEvel,Level)
     If Angle=0
        ThisColour1=RndRGB()
        ThisColour2=RndRGB()
     EndIf
     
     // First bank of 1024 colours
     Palette(lp)               =RgbAlphaMult(ThisColour1,Level)
     
     // treat colours 1024-2048 as bank 2 of colours
     Palette(1024+lp)     =RgbAlphaMult(ThisColour2,Level)
  Next
  
  
  
  // -----------------------------------------------------------------
  // -----------------------------------------------------------------
  // Create a PB image we'll be using as the Palette Mapped Display
  // -----------------------------------------------------------------
  // -----------------------------------------------------------------
  Screen=NewPaletteMapImage(GetScreenWidth(),GetScreenHeight())
  
  
  // -----------------------------------------------------------------
  //
  // -----------------------------------------------------------------
  RenderToImage Screen
  LockBuffer
  thisrgb=Point(0,0)
  
  CentX=GetScreenWidth()/2
  CentY=GetScreenHeight()/2
  
  For ylp=0 To GetScreenHeight()/2-1
     For Xlp=0 To GetScreenWidth()-1
        Dist=GetDistance2D(xlp,ylp,CentX,CentY)
        PaletteMappedDot(xlp,ylp,Dist*1.5)
     Next
     CopyRect Screen,0,ylp,GetScreenWidth(),ylp+1,Screen,0,599-ylp
  Next
  UnLockBuffer
  
  
  CopyOfScreen=GetFreeImage()
  CopyImage Screen,CopyOfScreen
  
  
  
  Do
     
     // direct all Pb rendering to out screen image
     RenderToImage Screen
     
     // draw a clone of the screen image to our screen
     DrawImage CopyOfScreen,0,0,false
     
     mx =MouseX()
     my =MouseY()
     
     // Set the drawing pens ink mode to OR mode
     InkMode 1+512
     
     // draw circle over the frame.  So we're OR's the value
     // 1024 to the colour indexes in this frame.
     // So those pixels will now bew drawn in colours 1024 to 2048
     // instead of 00 to 1024
     // this creates shadow effect where the colours in the circle
     // display as completely different colours.
     
     CircleC mx,my,200,true, indexToRgb(1024)
     
     // restore normal ink mode
     InkMode 1
     
     // render our palette mapped screen to the actual screen
     RenderToScreen
     RenderPaletteMapImage(Screen,0,0)
     
     // show everything to the user
     Sync
  Loop EscKey()=true
  
  
  
  
  





Example #2: Palette Map Sprite Sceene example




 
Example Source: Download This Example
  // Include the palette Mapping library
  #Include "PaletteMapping"
  
  
  // -----------------------------------------------------------------
  // -----------------------------------------------------------------
  // Set Up Palette >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  // -----------------------------------------------------------------
  // -----------------------------------------------------------------
  
  // Define a palette big enough to hold 2^16 colours
  Dim Palette($10000)
  
  // Give palette mapping library address of the palette
  // we're using.
  SetPalette(Palette())
  
  // Set Colour Index zero to rgb(0,0,0) BLACK
  Palette(0)     = RGB(0,0,0)
  
  // Set Colour as out 2 as
  Palette(1)     = RGB(30,40,50)
  
  // This number of colours currently in our palette
  ColourCount=2
  
  // Load the Ship image from the media folder to folders down
  ShipImage=LoadNewImage(ProgramDir$()+"Help/Commands/Media/ship.bmp")
  
  
  CenterText 250,80,"ORIGINAL RGB IMAGE"
  DrawImage ShipImage,200,100,false3
  
  // Convert the images normally RGB formated pixels into our
  // current palette
  ColourCount=PaletteMapRGBImage(ShipImage,Palette(), ColourCount)
  CenterText 600,80,"Palette Mapped IMAGE"
  CenterText 600,170,"Colour Count:"+Str$(ColourCount)
  
  DrawImage ShipImage,550,100,false
  
  
  
  Sync
  Wait 1000
  Wait 1000
  
  
  // -----------------------------------------------------------------
  // -----------------------------------------------------------------
  // Create a PB image we'll be using as the Palette Mapped Display
  // -----------------------------------------------------------------
  // -----------------------------------------------------------------
  Screen=NewPaletteMapImage(GetScreenWidth(),GetScreenHeight())
  
  
  Dim BackupPalette($10000)
  CopyArray Palette(),BackUpPalette()
  
  // Limit the program to 75 frames per second or less
  SetFPS 75
  
  
  Do
     
     // direct all Pb rendering to out screen image
     RenderToImage Screen
     
     
     // Fill The Screen with colour index 1.
     // CLS doesn't understand palette mapping, so we have to
     // convert our colur index into RGB form for drawing to
     // the screen
     Cls IndexToRgb(1)
     
     
     // draw our grid of ship images to the screen at the mouses
     // position
     GridImage ShipImage,MouseX(),MouseY(),10,10,true
     
     
     // Draw a rotated version of the palette mapped ship image
     DrawRotatedImage ShipImage,400,300,FadeAngle#,1,1,0,0,true
     
     
     // render our palette mapped screen to the actual screen
     RenderToScreen
     DrawPaletteMapImage(Screen,0,0)
     
     // --------------------------------------
     // Fade palette in and out
     // --------------------------------------
     ScaleLevel=128+Cos(Fadeangle#)*128
     ScaleLevel=ClipRange(ScaleLevel,0,255)
     ShadeRGB=RGB(ScaleLevel,ScaleLevel,ScaleLevel)
     
     For lp =1 To ColourCount
        Palette(lp)=RgbAlphaMult(BackupPalette(lp),ShadeRGB)
     Next
     
     Fadeangle#=WrapAngle(Fadeangle#+1)
     
     
     // show everything to the user
     Sync
  Loop EscKey()=true
  
  
  
  
 
Related Info: CreateFxImageEx | DeleteIMage | DrawPaletteMappedStrip16 | IndexToRGB | PaletteMapRGBImage :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com